package ug.co.icom.kyazze.healthticket.mongoHQ;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import ug.co.icom.kyazze.healthticket.data.ConnRequests;
import ug.co.icom.kyazze.healthticket.data.LoginReturnValue;
import ug.co.icom.kyazze.healthticket.data.MinUser;
import ug.co.icom.kyazze.healthticket.data.Permissions;
import ug.co.icom.kyazze.healthticket.data.QueryBuilder;
import ug.co.icom.kyazze.healthticket.data.User;
import android.os.AsyncTask;

public class MongoHQGetConnectionRequests extends AsyncTask<String, Void, ArrayList<ConnRequests>> {

	static BasicDBObject user = null;
	static String OriginalObject = "";
	static String server_output = null;
	static String temp_output = null;
	
	Locale locale = Locale.getDefault();

	@Override
	protected ArrayList<ConnRequests> doInBackground(String... params) {
		ArrayList<ConnRequests> lrv = new ArrayList<ConnRequests>();
		try {
			
			
			String phoneNumber =params[0];
			QueryBuilder qb = new QueryBuilder();
			URL url = new URL(qb.buildGetConnectionRequestsUrl(phoneNumber));
			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			//System.out.println("Output from Server .... \n");
			while ((temp_output = br.readLine()) != null) {
				server_output = temp_output;
			}

			// Remove square brackets
			OriginalObject = server_output.substring(1, server_output.length() - 1);
			
			DBObject mainObj = (DBObject) com.mongodb.util.JSON.parse(OriginalObject);

			BasicDBList dblist = (BasicDBList) mainObj.get("connrequests");

			for(Object ob: dblist)
			{
			DBObject userObj = (DBObject) ob;
			
			Permissions perm = new Permissions();
			
			perm.Allergies = Integer.parseInt(userObj.get("allergies").toString());
			perm.Conditions = Integer.parseInt(userObj.get("conditions").toString());
			perm.DateOfBirth = Integer.parseInt(userObj.get("dateofbirth").toString());
			perm.Imaging = Integer.parseInt(userObj.get("imaging").toString());
			perm.Immunization = Integer.parseInt(userObj.get("immunization").toString());
			perm.Insurance = Integer.parseInt(userObj.get("insurance").toString());
			perm.LabData = Integer.parseInt(userObj.get("labdata").toString());
			perm.Medication = Integer.parseInt(userObj.get("medication").toString());
			perm.PhoneNumber = Integer.parseInt(userObj.get("phonenumber").toString());
			perm.Surgeries = Integer.parseInt(userObj.get("surgeries").toString());
			perm.ID = userObj.get("uid").toString();
			
			MinUser temp = new MinUser();

			temp.ID = userObj.get("uid").toString();
			temp.FirstName = userObj.get("firstname").toString();
			temp.LastName = userObj.get("lastname").toString();
			temp.OtherNames = userObj.get("othernames").toString();

			ConnRequests cr = new ConnRequests();
			cr.permissions = perm;
			cr.user = temp;
			lrv.add(cr);
			}

		}catch (Exception e) {
			
			String t = e.getMessage();
			return lrv;
		}
		return lrv;
	}

	@Override
	protected void onPostExecute(ArrayList<ConnRequests> result) {
		super.onPostExecute(result);
	}
	
}
